package utils;


import android.app.Activity;

import java.util.Stack;


public class ActivityStack extends Stack<Activity> {
    private static ActivityStack instance;

    private ActivityStack() {
    }

    public static ActivityStack get() {
        if (instance == null) {
            instance = new ActivityStack();
        }
        return instance;
    }

    // 退出栈顶Activity
    public void removeActivity() {
        Activity activity = lastElement();
        if (activity != null) {
            remove(activity);
            activity.finish();

        }
    }

    // 将Activity移除
    public void removeActivity(Activity activity) {
        if (activity != null) {
            remove(activity);
            activity.finish();
        }

    }


    // 退出栈中所有除了cls的Activity
    @SuppressWarnings("rawtypes")
    public void removeOther(Class cls) {
        if (!isEmpty()) while (true) {
            Activity activity = lastElement();
            if (!activity.getClass().equals(cls)) removeActivity(activity);
        }


    }

    // 退出栈中所有Activity
    public void removeAllActivity() {
        while (true) {
            Activity activity = lastElement();
            if (activity == null) {
                break;
            }
            removeActivity(activity);
        }
    }

    public void exit() {
        removeAllActivity();
        try {
            android.os.Process.killProcess(android.os.Process.myPid());    //获取PID
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }

        //常规java、c#的标准退出法，返回值为0代表正常退出
    }
}